import {defineStore} from 'pinia';
import {Classify} from "~/entity/classify";
import {Response} from "@/entity/response";

export const classify = defineStore('classify', {
    state: () => ({
        classifyData: [] as Classify[],
    }),
    getters: {},
    actions: {
        setClassifyData(classifyData: Classify[]) {
            if (classifyData) {
                this.classifyData = classifyData;
            }
        },
        getClassifyData() {
            useApi().classifyApi.getList().then((res: any) => {
                let { data }: Response = res;
                let arr = data || [];
                this.setClassifyData(arr);
            });
        },
        getClassifyDataAndCollect() {
            let arr = this.classifyData;
            let newArr = [];
            const {loginStatus} = useStoreToRefs(useStores.user());
            if (loginStatus.value) {
                newArr.push({
                    classifyName: "我的收藏"
                });
            }
            if (arr && arr.length > 0) {
                arr.forEach((item: any) => {
                    newArr.push(item);
                });
            }
            return newArr;
        }
    },
    persist: true,
})